package spindles.gwt.client;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ProgressBarHelper {
	
	private ProgressBar bar = new ProgressBar(0.0, 2000.0, 0.0);
	
	private Boolean progressBarVisible = Boolean.TRUE;
	
	private Boolean initializing = new Boolean(true);
	
	private ProgressHandler handler;
	
	private String message;
	
	private boolean onlyWidget;
	
	private Timer pollTimer;
	
	public ProgressBarHelper(ProgressHandler handler, String message){
		this(handler, message, false);
	}
	
	public ProgressBarHelper(ProgressHandler handler, String message, boolean onlyWidget){
		this.handler = handler;
		this.message = message;
		this.onlyWidget = onlyWidget;
	}
	
	public void initPollTimer(){
		pollTimer = new Timer(){
			public void run() {
				checkProgress();
			}
		};

		pollTimer.scheduleRepeating(1000 * 15);
	}
	
	public void pollServer(int delayMillis){
		pollTimer.schedule(delayMillis);		
	}
	
	public void checkProgress(){
		Callback command = new Callback() {
			public void execute(Object result) {				
				boolean isImporting = ((Boolean)result).booleanValue();
				
				if ((isImporting && !progressBarVisible.booleanValue()) ||
				   (isImporting && initializing.booleanValue())){
					handler.showProgressBar(createProgressBar(message));
					runProgressBar();
					progressBarVisible = Boolean.TRUE;
				} else if (!isImporting && progressBarVisible.booleanValue()) {
					handler.hideProgressBar();
					progressBarVisible = Boolean.FALSE;

				}
				
				initializing = Boolean.FALSE;
							
			}			
		};
		
		ServerGateway.isImporting(Spindles.createCallBack(command));		
	}
	
	public Panel createProgressBar(String messageText){
		VerticalPanel outer = new VerticalPanel();
			
		DockPanel mainPanel = new DockPanel();
		
		VerticalPanel widgetPanel = new VerticalPanel();
		
		Label message = 
			new Label(messageText);
		message.setHorizontalAlignment(Label.ALIGN_CENTER);
		
		bar.setTextVisible(false);
		
		widgetPanel.add(new Label(""));
		widgetPanel.add(message);
		widgetPanel.add(new Label(""));
		widgetPanel.add(bar);
		widgetPanel.setCellHorizontalAlignment(bar, VerticalPanel.ALIGN_CENTER);
		widgetPanel.setCellVerticalAlignment(bar, VerticalPanel.ALIGN_MIDDLE);
		widgetPanel.setWidth("500px");		
		
		mainPanel.add(widgetPanel, DockPanel.CENTER);
		mainPanel.setCellHorizontalAlignment(widgetPanel, VerticalPanel.ALIGN_CENTER);
		mainPanel.setCellVerticalAlignment(widgetPanel, VerticalPanel.ALIGN_MIDDLE);
		mainPanel.setWidth("100%");
		mainPanel.setHeight("100%");
		
		
		outer.add(mainPanel);			
		outer.setWidth("100%");		
		
		if(onlyWidget){
			return outer;
		}
		return mainPanel;		
	}
	
	public void runProgressBar() {
		Timer t = new Timer() {
			public void run() {
				if(bar.getPercent() >= 1.0){
					bar.setProgress(0);
				} else{
					bar.setProgress(bar.getProgress() + 100);
				}
				
			}
		};

		t.scheduleRepeating(200);
	}
	
	

}
